package com.cserver.pro.utils;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.io.IOException;

public class Test {
    private static  final String ACTIVEMQ_URL="tcp://127.0.0.1:61616";
    private static final String QUEUE_NAME="queue01";
    public static void main(String[] args) throws JMSException, InterruptedException, IOException {



         new Thread() {

            @Override
            public void run() {
                try {
                    getCusomes("queue01","我是1号");
                    new Thread(){
                        @Override
                        public void run() {
                            try {
                                getCusomes("queue01","我是2号");
                            } catch (JMSException e) {
                                e.printStackTrace();
                            }
                        }

                    }.start();

                } catch (JMSException e) {
                    e.printStackTrace();
                }

            }
        }.start();
       // getCusomes2("queue01","1hao");
       // getCusomes2("queue01","2hao");
        producerMessage("queue01");
        producerMessage("queue01");
//        System.out.println("xiaofeiqian");
//        Thread.sleep(3000);
//        System.out.println("xiaofeihou");
//        producerMessage("queue02");
//        getCusomes("queue02");
    }
    public static void producerMessage(String queueName) throws JMSException {
            ActiveMQConnectionFactory activeMQConnectionFactory=new ActiveMQConnectionFactory(ACTIVEMQ_URL);
            Connection connection = activeMQConnectionFactory.createConnection();
            connection.start();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            Queue queue = session.createQueue(queueName);
            MessageProducer producer = session.createProducer(queue);
            for (int i=1;i<=4;i++){
                TextMessage textMessage = session.createTextMessage("xiaoxi"+i);
                producer.send(textMessage);
                System.out.println("生成消息");
            }
            producer.close();
            session.close();
            connection.close();

        }

        public static void getCusomes(String queueName,String name) throws JMSException {
            System.out.println("消费"+name);
            ActiveMQConnectionFactory activeMQConnectionFactory=new ActiveMQConnectionFactory(ACTIVEMQ_URL);
            Connection connection = activeMQConnectionFactory.createConnection();
            connection.start();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            Queue queue = session.createQueue(queueName);
            MessageConsumer messageConsumer = session.createConsumer(queue);
            while(true){
                TextMessage textMessage = (TextMessage) messageConsumer.receive();
                if(null!=textMessage){
                    System.out.println(textMessage.getText());
                }else{
                    System.out.println("跳出循环");
                    break;
                }
            }
            System.out.println("chulai");
            messageConsumer.close();
            session.close();
            connection.close();

        }


    public static void getCusomes2(String queueName,String name) throws JMSException, IOException {
        System.out.println("消费"+name);
        ActiveMQConnectionFactory activeMQConnectionFactory=new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        Connection connection = activeMQConnectionFactory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue(queueName);
        MessageConsumer messageConsumer = session.createConsumer(queue);
        messageConsumer.setMessageListener(new MessageListener() {
            @Override
            public void onMessage(Message message) {
                if(null!=message && message instanceof TextMessage){
                    TextMessage textMessage= (TextMessage) message;
                    try {
                        System.out.println("消息来了"+textMessage.getText());
                    } catch (JMSException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        System.in.read();
        System.out.println("chulai");
        messageConsumer.close();
        session.close();
        connection.close();

    }
}
